﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TP2
{
    public partial class EditAccumLegalClientForm : Form
    {
        string client_type;
        AccumulationForm accumForm;
        int id_client;
        int deposit;
        Double procent;
        int numb;
        bool flag;
        public EditAccumLegalClientForm(string clientType,AccumulationForm Fr,int idClient,int Deposit,Double Procent,int Numb)
        {
            client_type = clientType;
            accumForm = Fr;
            id_client = idClient;
            deposit = Deposit;
            procent = Procent;
            numb = Numb;
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int translate = Convert.ToInt32(textBoxTranslate.Text);
                deposit = deposit + Convert.ToInt32(textBoxAdd.Text) - Convert.ToInt32(textBoxSubtract.Text) - translate;
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.UpdateAccumulation(deposit, flag, period, dateTimePicker1.Value, numb, procent);
                accumForm.RefreshTable();
                if ((deposit >= 0) && (translate > 0))
                {
                    dbWork.UpdateFromPayments(id_client, translate);
                }
                MessageBox.Show("операция произведена");
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных"); }
        }

        private void buttonTranslate_Click(object sender, EventArgs e)
        {
            PaymentForm payForm = new PaymentForm(id_client);
            payForm.Show();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EditAccumLegalClientForm_Load(object sender, EventArgs e)
        {
            labelDeposit.Text = deposit.ToString();
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selectRate = new ClassRateType();
            selectRate = dbWork.SelectFromAccumulationRate(client_type);
            if (selectRate.id_rate.Count == 0) { dataGridView1.RowCount = 1; } else { dataGridView1.RowCount = selectRate.id_rate.Count; }
            int j = 0;
            while (j < selectRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = selectRate.title[j].ToString();
                dataGridView1[1, j].Value = selectRate.procent[j].ToString();
                dataGridView1[2, j].Value = selectRate.period[j].ToString();
                j++;
            }      

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                flag = true;
                dateTimePicker1.Enabled = true;
                dataGridView1.Enabled = true;
            }
            else
            {
                flag = false;
                dateTimePicker1.Enabled = false;
                dataGridView1.Enabled = false;
            }
        }
    }
}
